package com.traffic.test.base;


import java.util.HashMap;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;

/**
 * Created by wanghongyue on 2017/5/16.
 */
public class ThreadDemo {
    private static HashMap<String, String> hashMap = new HashMap<>();

    //线程池为无限大，当执行第二个任务时第一个任务已经完成，会复用执行第一个任务的线程，而不用每次新建线程。

    public static void main1(String[] args) {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            try {
                Thread.sleep(index * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cachedThreadPool.execute(new Runnable() {
                public void run() {
                    System.out.println(index);
                }
            });
        }
    }

    public static void main2(String[] args) {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            final int index = i;
            fixedThreadPool.execute(new Runnable() {
                public void run() {
                    try {
                        System.out.println(index);
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    public static void main3(String[] args) {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            public void run() {
                System.out.println("delay 1 seconds, and excute every 3 seconds");
            }
        }, 1, 3, TimeUnit.SECONDS);
    }


    public void main23() {
        new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println("456");
                try {
                    this.notify();
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("789");
            }
        }).start();
    }
    public static void main(String[] args) {

        while (true) {
            new Thread("Thread1"){
                @Override
                public void run() {
                    hashMap.put("1", "11");
                }
            }.start();
            new Thread("Thread2"){
                @Override
                public void run() {
                    hashMap.put("2", "22");
                }
            }.start();
            new Thread("Thread3"){
                @Override
                public void run() {
                    hashMap.put("3", "33");
                }
            }.start();
            new Thread("Thread4"){
                @Override
                public void run() {
                    hashMap.put("4", "44");
                }
            }.start();
            System.out.println(hashMap);

        }


    }
}
